package pub.tbc.atps.util.okhttp;

import com.squareup.okhttp.Headers;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import lombok.extern.slf4j.Slf4j;
import pub.tbc.toolkit.Loops;
import pub.tbc.toolkit.function.BiConsumer;

import java.io.IOException;
import java.util.Map;

/**
 * @author tbc on 2016/12/2 12:18:03.
 */
@Slf4j
public class OkHttpRequester {
    public static Response get(String url, Map<String, String> headers, Map<String, Object> param) {
        try {
            return new OkHttpClient()
                    .newCall(
                            new Request.Builder()
                                    .url(url)
                                    .method("get", null)
                                    .headers(getHandlers(headers))
                                    .build()
                    ).execute();
        } catch (IOException e) {
            log.error("请求第三方系统异常： {}", e.getMessage());
            throw new RuntimeException("请求第三方系统异常：" + e.getMessage(), e);
        }
    }

    private static Headers getHandlers(Map<String, String> headers) {
        Headers.Builder hs = new Headers.Builder();
        Loops.forEach(headers, new BiConsumer<String, String>() {
            @Override
            public void accept(String k, String v) {
                hs.add(k, v);
            }
        });
        return hs.build();
    }

    public static String getPathParam(String url, Map<String, Object> param) {

        return null;
    }

    public static String getHttpParam(String url, Map<String, Object> param) {

        return null;
    }

    public static Response put(String url, Map<String, String> headers, Map<String, Object> param) {

        return null;
    }

    public static Response post(String url, Map<String, String> headers, Map<String, Object> param) {

        return null;
    }

    public static Response delete(String url, Map<String, String> headers, Map<String, Object> param) {

        return null;
    }

    public Response request(String url, String method, Map<String, String> headers, Map<String, Object> param) {

        return null;
    }


}
